********************************************************************************
*********			  		  FIG 5 Panel A - ESTIMATES OF THE 
*********						VALUE OF URGENCY OVER THE AM PEAK      	********
********************************************************************************

use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

merge m:1 date hour using ".\data\clean\\HV_ML_reliab.dta", keep(1 3) nogen 
gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV

*WTP calculation
gen TT_dif_hr=dist/MLspeed-dist/ELspeed
gen WTP2=charged_toll/TT_dif_hr
drop if ELspeed==.
drop if TT_dif_hr==.
drop if holiday==1
la var TT_dif_hr "Time in Hours"
la var reliability_diff "Reliability"
drop if dow==0|dow==6
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
keep if hour>4 & hour<9
replace reliability_diff=0 if reliability_diff<0.01
g exittime=hh(exit_time)+mm(exit_time)/60+ss(exit_time)/(60*60)
egen exittimemean=mean(exittime), by(acct_no)
g exittimediff=exittime-exittimemean
g TT_dif_hr2 =TT_dif_hr^2
egen t=group(hour min)
qui tab t, g(td)
reg charged_toll TT_dif_hr reliability_diff td* if TT_dif_hr>0 , cluster(rt_id) nocons
local df=`e(df_r)'
forval i=1/48{
	local beta`i'=_b[td`i']
	local se`i'=_se[td`i']
	}
preserve
clear
set obs 48
g beta=.
g ci1=.
g ci2=.
g t=.
forval i=1/48{
	replace beta= `beta`i'' in `i'
	replace ci1=`beta`i''+invttail(`df',.025)*`se`i'' in `i'
	replace ci2=`beta`i''-invttail(`df',.025)*`se`i'' in `i'
	replace t=`i' in `i'
	}
g time=5+(t*5)/60
twoway (connect beta time, lp(solid) lw(medthick) lc(black) m(o) mc(black)) (line ci1 time, ///
	lp(dash) lc(gray))  (line ci2 time, lp(dash) lc(gray)) ///
	,xlabel(5 "5AM" 6 "6AM" 7 "7AM" 8 "8AM" 9 "9AM") ytitle("Value of Urgency in Dollars") ///
	xtitle("") plotregion(style(none)) legend(off)
graph export ".\results\maintablefigs\figs\fig5pa.png", width(2550) replace
restore
putexcel set ".\results\appendix\AppendixFigsTabs.xlsx", sheet("Appendix Figure B.4") modify
  putexcel A1 = picture(".\results\maintablefigs\figs\fig5pa.png")
